package io.renren.modules.task.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.task.entity.MtCcEquipmentRunInfoEntity;
import io.renren.modules.task.entity.MtCcEquipmentRunInfoLogEntity;
import io.renren.modules.task.service.MtCcEquipmentRunInfoLogService;
import io.renren.modules.task.service.MtCcEquipmentRunInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Author: weihongyang
 * @Date: 2019/5/22 9:52
 * @Version 1.0
 */
@RestController
@Api(description = "agv回调接口API")
@RequestMapping("/agv/call/back")
public class AgvCallBackController extends AbstractController {

    @Autowired
    private MtCcEquipmentRunInfoService mtCcEquipmentRunInfoService;

    @Autowired
    private MtCcEquipmentRunInfoLogService mtCcEquipmentRunInfoLogService;

    @PostMapping("/task")
    @ApiOperation(value = "agv任务通知操作测试", notes = "agv任务通知操作测试")
    public R agvCallBack1(@RequestBody Map<String, Object> params){
        System.out.println(params.get("reqCode").toString());
        return R.ok();
    }
    @PostMapping("/task/end")
    @ApiOperation(value = "agv任务通知操作", notes = "agv任务通知操作")
    public R agvCallBack(@RequestBody Map<String, Object> params){
        String reqCode = params.get("reqCode").toString();
        String format = "yyyy-MM-dd HH:mm:ss";
        try {
            Date reqTime = new SimpleDateFormat(format).parse(params.get("reqTime").toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String method = params.get("method").toString();
        System.out.println(method);
        String robotCode = params.get("robotCode").toString();
        String taskCode = params.get("taskCode").toString();
        MtCcEquipmentRunInfoEntity mtCcEquipmentRunInfoEntity =
                mtCcEquipmentRunInfoService.getOne(new QueryWrapper<MtCcEquipmentRunInfoEntity>().eq("task_code",
                        taskCode));
        Integer taskType = mtCcEquipmentRunInfoEntity.getTaskType();
        Integer status = mtCcEquipmentRunInfoEntity.getStatus();
        MtCcEquipmentRunInfoLogEntity mtCcEquipmentRunInfoLogEntity = new MtCcEquipmentRunInfoLogEntity();
        switch (method){
            case "start":
                mtCcEquipmentRunInfoEntity.setStatus(2);
                mtCcEquipmentRunInfoService.update(mtCcEquipmentRunInfoEntity,
                                new QueryWrapper<MtCcEquipmentRunInfoEntity>().eq("task_code",taskCode));
                break;
            case "end":
                mtCcEquipmentRunInfoLogService.update(mtCcEquipmentRunInfoLogEntity,new QueryWrapper<MtCcEquipmentRunInfoLogEntity>().eq(
                            "task_code",taskCode));
                mtCcEquipmentRunInfoService.remove(new QueryWrapper<MtCcEquipmentRunInfoEntity>().eq(
                        "task_code",taskCode));
                break;
            default:
                break;
        }
        return R.ok();
    }
}
